﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace TasksManagerServices.Utilities
{
    public static class DatabaseHelper
    {
        public static MySqlConnection GetTaskManagementConnection()
        {
            try
            {
                var connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["TaskManagement"].ConnectionString);
                connection.Open();
                return connection;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static MySqlDataReader GetMysqlCommandReader(string query, MySqlConnection connection)
        {
            try
            {
                return new MySqlCommand(query, connection).ExecuteReader();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static void DisposeConnection(MySqlConnection connection, MySqlDataReader reader = null)
        {
            try
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }

                if (connection.State != ConnectionState.Closed)
                {
                    connection.Clone();
                }
                connection.Dispose();             
            }
            catch (Exception)
            {
            }
        }
    }
}
